//
//  HelpDetailViewController.swift
//  Exchange
//
//  Created by rui on 2018/7/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class HelpDetailViewController: BaseViewController {
    
    let reuseIdentifier = "HelpTableViewCell"
    
    var model: HelpCenterModel?
    
    lazy var viewModel: HelpDetailViewModel = {
        let vm = HelpDetailViewModel()
        vm.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.mine_help_detail_list {
                if isSuccess {
                    self?.tableView.reloadData()
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }
        }
        return vm
    }()

    lazy var tableView: UITableView = {
        let table = UITableView(frame: self.view.bounds, style: .plain)
        table.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        table.delegate = self
        table.dataSource = self
        table.rowHeight = 55
        table.themeBackground = ThemeManager.ThemeColorKeys.background
        table.register(UINib(nibName: reuseIdentifier, bundle: nil), forCellReuseIdentifier: reuseIdentifier)
        table.tableFooterView = UIView()
        table.themeSeparatorColor = ThemeManager.ThemeColorKeys.separator
        return table
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initData()
    }
    
    func initUI() {
        self.automaticallyAdjustsScrollViewInsets = false
        self.title = model?.catName
        self.view.addSubview(tableView)
    }
    
    func initData() {
        self.viewModel.loadHelpDetailList(id: model?.id ?? 0)
    }
    
}

extension HelpDetailViewController: UITableViewDelegate,UITableViewDataSource {
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! HelpTableViewCell
        let model = self.viewModel.dataArray[indexPath.row]
        cell.title.text = model.title
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        let model = self.viewModel.dataArray[indexPath.row]
        let vc = HelpContentViewController()
        vc.model = model
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
